package org.iks.store.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.iks.common.pojo.message.ChatMessage;
import org.iks.common.pojo.message.ChatMessageStoreVo;
import org.iks.common.pojo.store.SecretKeyVo;
import org.iks.common.pojo.user.UserInfoVo;
import org.iks.common.util.ServletUtil;
import org.iks.feign.service.user.UserRemoteService;
import org.iks.store.business.entity.SessionMessageEntity;
import org.iks.store.business.entity.SessionUserMessageEntity;
import org.iks.store.business.mapper.SessionMessageMapper;
import org.iks.store.business.service.SessionEncipherService;
import org.iks.store.business.service.SessionMessageService;
import org.iks.store.business.service.SessionUserMessageService;
import org.iks.store.business.vo.MessageQueryVo;
import org.iks.store.business.vo.message.MessageAnchorQueryVo;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Author kzou
 * Date 2024/2/14
 * Description:
 */
@Slf4j
@Service
public class SessionMessageServiceImpl extends ServiceImpl<SessionMessageMapper, SessionMessageEntity>
        implements SessionMessageService {

    private final SessionUserMessageService userMessageService;
    private final UserRemoteService userRemoteService;
    private final SessionEncipherService encipherService;

    public SessionMessageServiceImpl(
            SessionUserMessageService userMessageService,
            UserRemoteService userRemoteService,
            SessionEncipherService encipherService) {
        this.userMessageService = userMessageService;
        this.userRemoteService = userRemoteService;
        this.encipherService = encipherService;
    }

    @Override
    public List<ChatMessage> querySessionMessage(MessageQueryVo queryVo, String currAccount) {
        List<ChatMessage> result = baseMapper.querySessionMessage(queryVo);
        for (ChatMessage chatMessage : result) {
            String senderAccount = chatMessage.getSenderAccount();
            UserInfoVo userInfoVo = userRemoteService.queryUserInfo(senderAccount).getData();
            SecretKeyVo secretKeyVo = encipherService.queryByVersion(
                    queryVo.getSessionId(),
                    chatMessage.getSecretVersion(),
                    currAccount
            );
            chatMessage.setSecretKeyVo(secretKeyVo);
            chatMessage.setUserInfoVo(userInfoVo);
        }
        return result;
    }

    @Override
    public ChatMessage queryLatestMessage(Integer sessionId) {
        String userAccount = ServletUtil.currUser().account();
        ChatMessage chatMessage = baseMapper.queryLatestMessage(sessionId);
        if (chatMessage==null){
            return null;
        }
        UserInfoVo infoVo = userRemoteService.queryUserInfo(chatMessage.getSenderAccount()).data();
        SecretKeyVo secretKeyVo = encipherService.queryByVersion(sessionId, chatMessage.getSecretVersion(), userAccount);
        chatMessage.setUserInfoVo(infoVo);
        chatMessage.setSecretKeyVo(secretKeyVo);
        return chatMessage;
    }

    @Override
    public void saveMessage(ChatMessageStoreVo message) {
        String account = ServletUtil.currUser().account();
        SessionMessageEntity sessionMessageEntity = new SessionMessageEntity();
        sessionMessageEntity.setSenderAccount(account);
        sessionMessageEntity.setMessage(message.getContent());
        sessionMessageEntity.setMessageType(message.getChatMessageType());
        sessionMessageEntity.setCreateTime(new Date());
        sessionMessageEntity.setSessionId(message.getSessionId());
        sessionMessageEntity.setSecretVersion(message.getSecretVersion());
        baseMapper.insert(sessionMessageEntity);
    }

    @Override
    public int readMessage(String userAccount, Integer messageId) {
        SessionUserMessageEntity entity = new SessionUserMessageEntity();
        entity.setUserAccount(userAccount);
        entity.setMessageId(messageId);
        try {
            return userMessageService.save(entity) ? 1 : 0;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public ChatMessage queryById(Integer messageId, String currAccount) {
        ChatMessage message = baseMapper.queryMessageById(messageId);
        if (message==null){
            return null;
        }
        String senderAccount = message.getSenderAccount();
        UserInfoVo userInfoVo = userRemoteService.queryUserInfo(senderAccount).getData();
        SecretKeyVo secretKeyVo = encipherService.queryByVersion(message.getSessionId(), message.getSecretVersion(), currAccount);
        message.setUserInfoVo(userInfoVo);
        message.setSecretKeyVo(secretKeyVo);
        return message;
    }

    @Override
    public List<ChatMessage> querySessionMessageByAnchor(String currAccount, MessageAnchorQueryVo queryVo) {
        List<ChatMessage> result = baseMapper.querySessionMessageByAnchor(queryVo);
        for (ChatMessage chatMessage : result) {
            String senderAccount = chatMessage.getSenderAccount();
            UserInfoVo userInfoVo = userRemoteService.queryUserInfo(senderAccount).getData();
            SecretKeyVo secretKeyVo = encipherService.queryByVersion(
                    queryVo.getSessionId(),
                    chatMessage.getSecretVersion(),
                    currAccount
            );
            chatMessage.setSecretKeyVo(secretKeyVo);
            chatMessage.setUserInfoVo(userInfoVo);
        }
        return result;
    }

}
